package com.notice.content;

import java.io.IOException;
//import java.lang.reflect.Field;
//import java.sql.Date;
//import java.text.SimpleDateFormat;
//import java.util.jar.Attributes.Name;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

//import com.example.logindesign.Mainmenu;
import com.example.logindesign.R;

//import android.R.anim;
//import android.app.ActionBar;
import android.app.Activity;
//import android.app.AlertDialog;
//import android.app.Dialog;
//import android.app.TaskStackBuilder;
//import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
//import android.support.v4.app.NavUtils;
//import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.KeyEvent;
//import android.view.Menu;
//import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
//import android.view.ViewConfiguration;
//import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
//import android.widget.Toast;

public class NoticeDetailActivity extends Activity {
	private static final String NAMESPACE = "http://tempuri.org/";
	private static final String ENDPOINT = "http://192.168.1.198:1536/CommunitySystemsicWsWF.asmx";

	//返回按钮
	private ImageButton btnBack;
	
	// textview控件 5个
	private TextView textViewTitle;
	private TextView textViewType;
	private TextView textViewTime;
	private TextView textViewName;
	private TextView textViewContent;
	
	// TextView的变量值
	private String unitcode;
	private String userid;
	private String notiId;
	private String username;
	private String textViewTitleValue = "";
	private String textViewTypeValue = "";
	private String textViewTimeValue = "";
	private String textViewNameValue = "";
	private String textViewContentValue = "";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_notice_detail);
		// sharedpreference中取得相应值
		getValueFromSharedPreferences();
		// 初始化TextView
		initTextView();
	}
	
	/**
	 * sharedpreference中取得相应值
	 */
	private void getValueFromSharedPreferences() {
		// TODO Auto-generated method stub
		SharedPreferences sharedPreferences = getSharedPreferences("UserInfo",
				Activity.MODE_PRIVATE);
		userid = sharedPreferences.getString("userid", "");
		unitcode = sharedPreferences.getString("unitcode", "");
		username = sharedPreferences.getString("username", "");
		notiId = sharedPreferences.getString("notiId", "");
	}
	
	/**
	 * 初始化TextView及button
	 */
	private void initTextView() {
		
		textViewTitle = (TextView) findViewById(R.id.textView_notice_Title);
		textViewType = (TextView) findViewById(R.id.textView_notice_Type);
		textViewTime = (TextView) findViewById(R.id.textView_notice_Time);
		textViewName = (TextView) findViewById(R.id.textView_notice_Name);
		textViewContent = (TextView) findViewById(R.id.textView_notice_Content);
		
		// 按钮
		btnBack = (ImageButton) findViewById(R.id.btn_notice_detail_back);
		// 返回按钮
		btnBack.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// 跳转到listview页面
				Intent intent = new Intent();
				intent.setClass(NoticeDetailActivity.this,
						NoticeListViewActivity.class);
				startActivity(intent);
			}
		});
		
		new setTextViewValueTask().execute();
	}
	
	/**
	 * 设置textView的值
	 */
	private class setTextViewValueTask extends AsyncTask<Void, Void, String> {

		protected void onPreExecute() {
			// 设置TextView的值
			setTextViewValue();
		}

		@Override
		protected String doInBackground(Void... params) {
			// TODO Auto-generated method stub
			// 事件详细参数需要 id,unitcode
			Intent getdetail = getIntent();
			userid = getdetail.getStringExtra("userid");
			username = getdetail.getStringExtra("username");
			unitcode = getdetail.getStringExtra("unitcode");
			notiId = getdetail.getStringExtra("notiId");

			String resultString = getData();
			Log.e("webservice取得", "完毕");
			String finallyResultString = jsonFormat(resultString);
			return finallyResultString;
		}

		protected void onPostExecute(String finallyResultString) {
			if (finallyResultString.equals("success")) {
				// 设置TextView的值
				setTextViewValue();
			}
		}

	}
	
	
	/**
	 * 得到详细信息
	 * 
	 * @return
	 */
	private String getData() {
		// TODO Auto-generated method stub
		String resultString = "";
		String methodName = "get_SysNoticeDetails";
		String soapAcion = "http://tempuri.org/get_SysNoticeDetails";
		SoapObject soapObject = new SoapObject(NAMESPACE, methodName);

		// 通知通告id
		soapObject.addProperty("NotiId", notiId);
		// 操作人员id
		soapObject.addProperty("userid", userid);
		// 操作人员用户名
		soapObject.addProperty("username", username);
		// 操作人员所属区域编码
		soapObject.addProperty("unitcode", unitcode);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.setOutputSoapObject(soapObject);
		envelope.dotNet = true;
		HttpTransportSE transportSE = new HttpTransportSE(ENDPOINT);
		try {
			transportSE.call(soapAcion, envelope);
			if (envelope.getResponse() != null) {
				SoapObject object = (SoapObject) envelope.bodyIn;
				resultString = object.getProperty(0).toString();
				Log.e("webservice取得", resultString);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return resultString;
	}
	
	/**
	 * 解析json数据返回hashmap格式数据
	 * 
	 * @return
	 */
	private String jsonFormat(String resultString) {
		// TODO 由于事件管理详细webservice返回值多写了对中括号，导致数据崩溃，所以先用的固定值
//		resultString = "[{'Code':'1','Msg':'成功!'},{'unitcode':'3707250102','id':'12','shijianleixing':'突发事件','shijianleibie':'围堵单位','shejirenshu':'3','lianxifangshi':'15165605516','faxianchangsuo':'3#','dangshiren':'王瑞','shijianmiaoshu':'测试','dengjiren':'我自己','dengjishijian':'2014-07-23 15:04:57','suoshuwangge':'网格一','lururen':'测试','lurujigou':'故城社区','chuliyijian':'结案件'}]";
		JSONArray jsonArray;
		try {
			jsonArray = new JSONArray(resultString);
			JSONObject jsonObject = jsonArray.optJSONObject(0);
			if (jsonObject.optString("Code").equals("1")) {
				// 根据实际从第2组开始取得有关数据
				jsonObject = jsonArray.optJSONObject(1);
				textViewNameValue = jsonObject.optString("jieshouren");
				textViewContentValue = jsonObject.optString("NotiContent");
				textViewTimeValue = jsonObject.optString("createtime");
				textViewTypeValue = jsonObject.optString("NotiType");
				textViewTitleValue = jsonObject.optString("NotiTitle");
				return "success";
			} else {
				// 没有数据，如何显示？已经到最后了
				// map.put("ItemTime", R.drawable.icon_settings);
				// map.put("ItemTitle", "xin这是第" + mItemCount + "行");
				// map.put("ItemText", "xin这是第" + mItemCount + "行");
				return "fail";
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "fail";
	}
	
	
	/**
	 * 设置TextView的值
	 */
	private void setTextViewValue() {
		// TODO Auto-generated method stub
		textViewName.setText(textViewNameValue);
		textViewType.setText(textViewTypeValue);
		textViewTime.setText(textViewTimeValue);
		textViewTitle.setText(textViewTitleValue);
		textViewContent.setText(textViewContentValue);
	}
	
//	/**
//	 * 设定即使手机物理menu键，仍显示overflow按钮
//	 */
//	private void forceShowOverflowMenu() {
//		try {
//			ViewConfiguration config = ViewConfiguration.get(this);
//			Field menuKeyField = ViewConfiguration.class
//					.getDeclaredField("sHasPermanentMenuKey");
//			if (menuKeyField != null) {
//				menuKeyField.setAccessible(true);
//				menuKeyField.setBoolean(config, false);
//			}
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//	}
	
	/**
	 * 跳转到listview页面
	 */
	private void skipListViewActivity() {
		Intent listviewIntent = new Intent();
		listviewIntent.setClass(NoticeDetailActivity.this,
				NoticeListViewActivity.class);
		NoticeDetailActivity.this.startActivity(listviewIntent);
		NoticeDetailActivity.this.finish();
	}
	
	/**
	 * 重写keydown事件
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			skipListViewActivity();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
}
